package httpclient.gui;

import httpclient.engine.*;
import httpclient.engine.ui.BrowserWindow;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;



public class MainWindow extends javax.swing.JFrame
        implements BrowserWindow {

    private final HeadersWindow headersWindow;
    private final AboutDialog aboutDialog;
    private final OptionsDialog optionsDialog;
    private final AuthenticationDialog authDialog;
    private PreferenceList preferences;

    public MainWindow() {
        initComponents();
        Image appIcon = Toolkit.getDefaultToolkit().getImage(
                getClass().getResource("/httpclient/icons/application.png"));
        this.setIconImage(appIcon);
        preferences = new PreferenceList();
        headersWindow = new HeadersWindow();
        aboutDialog = new AboutDialog(MainWindow.this);
        optionsDialog = new OptionsDialog(MainWindow.this, preferences);
        authDialog = new AuthenticationDialog(MainWindow.this);

    }
    public static void main(String[] args) {
                java.awt.EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        new MainWindow().setVisible(true);
                    }
                });
                
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jURI = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jBrowserTextPane = new javax.swing.JTextPane();
        jGoButton = new javax.swing.JButton();
        jProgress = new javax.swing.JProgressBar();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jExitMenuItem = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jHeadersMenuItem = new javax.swing.JMenuItem();
        jMenuItemOptions = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jAboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("HTTP Client");

        jURI.setText("http://www.google.com.ua");
        jURI.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jURIKeyPressed(evt);
            }
        });

        jBrowserTextPane.setEditable(false);
        jBrowserTextPane.setDebugGraphicsOptions(javax.swing.DebugGraphics.BUFFERED_OPTION);
        jScrollPane1.setViewportView(jBrowserTextPane);

        jGoButton.setText("Go!");
        jGoButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jGoButtonMouseClicked(evt);
            }
        });

        jMenu1.setText("File");

        jExitMenuItem.setText("Exit");
        jExitMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jExitMenuItemMouseReleased(evt);
            }
        });
        jMenu1.add(jExitMenuItem);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Window");

        jHeadersMenuItem.setText("Headers");
        jHeadersMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jHeadersMenuItemMouseReleased(evt);
            }
        });
        jMenu3.add(jHeadersMenuItem);

        jMenuItemOptions.setText("Options");
        jMenuItemOptions.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jMenuItemOptionsMouseReleased(evt);
            }
        });
        jMenu3.add(jMenuItemOptions);

        jMenuBar1.add(jMenu3);

        jMenu2.setText("Help");

        jAboutMenuItem.setText("About");
        jAboutMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jAboutMenuItemMouseReleased(evt);
            }
        });
        jMenu2.add(jAboutMenuItem);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jGoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jURI, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jProgress, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jGoButton)
                        .addComponent(jURI, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jProgress, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

public void setProgress(int value){
    jProgress.setValue(value);
}

public void showMessage(String message){
    jBrowserTextPane.setForeground(Color.BLACK);
    jBrowserTextPane.setText(message);
}

public void showError(String message){
    jBrowserTextPane.setText(message);
    jBrowserTextPane.setForeground(Color.RED);
}

public Component getComponent(){
    return this;
}

public void enableGoButton(boolean setEnabled){
    jGoButton.setEnabled(setEnabled);
    jURI.setEnabled(setEnabled);
}


private void startEngine(){
    MainWindow mainWindow = this;
    String URI = jURI.getText();
    
    new Thread(new Engine(URI, preferences, mainWindow, headersWindow,
            authDialog)).start();
}

private void jGoButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jGoButtonMouseClicked
    startEngine();
}//GEN-LAST:event_jGoButtonMouseClicked

private void jHeadersMenuItemMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jHeadersMenuItemMouseReleased
    if(!headersWindow.isVisible()){
            headersWindow.setLocationRelativeTo(MainWindow.this);
            headersWindow.setVisible(true);
    }
}//GEN-LAST:event_jHeadersMenuItemMouseReleased

private void jExitMenuItemMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jExitMenuItemMouseReleased
    System.exit(0);
}//GEN-LAST:event_jExitMenuItemMouseReleased

private void jAboutMenuItemMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jAboutMenuItemMouseReleased
    aboutDialog.setLocationRelativeTo(MainWindow.this);
    aboutDialog.setVisible(true);
}//GEN-LAST:event_jAboutMenuItemMouseReleased

private void jMenuItemOptionsMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItemOptionsMouseReleased
    optionsDialog.setLocationRelativeTo(MainWindow.this);
    optionsDialog.setVisible(true);
}//GEN-LAST:event_jMenuItemOptionsMouseReleased

private void jURIKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jURIKeyPressed
    if(evt.getKeyCode()==KeyEvent.VK_ENTER){
        startEngine();
    }
}//GEN-LAST:event_jURIKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jAboutMenuItem;
    private javax.swing.JTextPane jBrowserTextPane;
    private javax.swing.JMenuItem jExitMenuItem;
    private javax.swing.JButton jGoButton;
    private javax.swing.JMenuItem jHeadersMenuItem;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemOptions;
    private javax.swing.JProgressBar jProgress;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jURI;
    // End of variables declaration//GEN-END:variables

}
